package com.toocms.jinjuhang.ui.mine.setting

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.User
import com.toocms.jinjuhang.ui.mine.setting.agreement.AgreementFgt
import com.toocms.jinjuhang.ui.mine.setting.feedback.FeedbackFgt
import com.toocms.tab.TooCMSApplication
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.widget.update.UpdateManager

class SettingModel(application: Application) : BaseViewModel<BaseModel>(application) {

    val versionCodeObservableField by lazy {
        val application = getApplication<TooCMSApplication>()
        val versionCode = application.packageManager
            .getPackageInfo(application.packageName, 0)
            .versionName
        ObservableField(versionCode)
    }

    val versionCodeClickBindingCommand=BindingCommand<BindingAction>(BindingAction{
        UpdateManager.checkUpdate()
    })

    val feedbackClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        startFragment(FeedbackFgt::class.java)
    })
    val userAgreementClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val params= Bundle()
        params.putString(Constants.BundleKey.KEY_TYPE,Constants.Extra.AGREEMENT_TYPE_USER)
        startFragment(AgreementFgt::class.java,params)
    })
    val privacyAgreementClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        val params= Bundle()
        params.putString(Constants.BundleKey.KEY_TYPE,Constants.Extra.AGREEMENT_TYPE_PRIVACY)
        startFragment(AgreementFgt::class.java,params)
    })
    val logoutClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        UserRepository.updateUser(User())
        UserRepository.setLogin(false)
        finishFragment()
    })
}